﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using smp.ButterflyCakes.Models.FormModels.Order;

namespace smp.ButterflyCakes.Models.InputModels.Order
{
    public class ShapeBuilderInput : IOrderModel
    {
        [Required]
        public Guid OrderId { get; set; }
        [Required]
        public Guid ComponentId { get; set; }
        public int Portions { get; set; }
        [Required]
        public int OverallLength { get; set; }
        [Required]
        public int OverallWidth { get; set; }
        [Required]
        public string SelectedCellString { get; set; }
        [Required]
        public string SizesString { get; set; }

        public List<int> SelectedCells
        {
            get
            {
                var list = new List<int>();
                foreach (var item in SelectedCellString.Split(','))
                {
                    int value;
                    if (int.TryParse(item, out value))
                    {
                        list.Add(value);
                    }
                }
                return list;
            }
        }

        public List<ShapeBuilderBake> Bakes
        {
            get
            {
                if (string.IsNullOrEmpty(SizesString))
                    throw new Exception("Invalid size string : " + SizesString);

                var sizes = SizesString.Split(',');
                if (sizes.Length == 0)
                    throw new Exception("Invalid size string : " + SizesString);

                var list = new List<ShapeBuilderBake>();
                foreach (var size in sizes)
                {
                    var values = size.Split('-');
                    if(string.IsNullOrEmpty(size) || values.Length != 2)
                        continue;

                    int length;
                    if (!int.TryParse(values[0], out length))
                        throw new Exception("Invalid size string : " + SizesString);

                    int width;
                    if (!int.TryParse(values[1], out width))
                        throw new Exception("Invalid size string : " + SizesString);
                    list.Add(new ShapeBuilderBake {Length = length, Width = width});
                }
                return list;
            }
        }
    }

    public class ShapeBuilderBake
    {
        public int Length { get; set; }
        public int Width { get; set; }
    }
}
